//
//  DYJingQuListViewController.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/11.
//  Copyright © 2021 叁拾叁. All rights reserved.
//


import UIKit
import MJRefresh
import RxSwift
import RxDataSources
import Then



class DYJingQuListViewController: SYBaseViewController, UICollectionViewDelegate, UICollectionViewDataSource {
    
    var collectionView: UICollectionView!
    fileprivate var vm: SYCollectionViewPageViewModel<DYJQYoulanListModel>!
    var id: String!
    
    init(id: String) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "景区游览"
        setCollectionView()
        setupViewModel()
        
    }
    
    fileprivate func setupViewModel() {
        
        vm = SYCollectionViewPageViewModel<DYJQYoulanListModel>()
        vm.collectionView = collectionView
        vm.emptyViewParams = ("", "", 40.0, 30.0)
        collectionView.vg_pageProtocol = vm
        vm.getList(type: .dy_jingquYoulanList(page: 1, size:10, dic: ["scenicId": id]), dispose: dispose)
        vm.getDataClosure = {[weak self] page in
            if let `self` = self {
                self.vm.getList(type: .dy_jingquYoulanList(page: self.vm.vg_pageNo, size:self.vm.size, dic: ["scenicId": self.id]), dispose: self.dispose)
            }
        }
        vm.loadDataClosure = {[weak self] success in
            if let `self` = self {
                if success {
                    self.loadDataSuccessed()
                }
            }
        }
    }
    
    func loadDataSuccessed() {
        DispatchQueue.main.async {
            
            self.collectionView.reloadData()
        }
    }

    
    func setCollectionView() {
        
            
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        let itemWidth = 170.0.autoWidth() + 5.0
        layout.itemSize = CGSize(width: itemWidth, height: 173.5.autoWidth())
//        layout.minimumLineSpacing = 10
//        layout.minimumInteritemSpacing = 10
        layout.scrollDirection = .vertical
        collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - BOTTOM_SAFE_HEIGHT), collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        
        
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(DYJingquYoulanSubCell.self, forCellWithReuseIdentifier: NSStringFromClass(DYJingquYoulanSubCell.self))
        self.view.addSubview(self.collectionView)
//        collectionView.backgroundColor = Constant.bgViewColor
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.vm.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: DYJingquYoulanSubCell? = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(DYJingquYoulanSubCell.self), for: indexPath) as? DYJingquYoulanSubCell
        cell?.configure(self.vm.dataSource[indexPath.row])
        
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = DYJingquJianjieViewController(m:self.vm.dataSource[indexPath.row])
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
    
    
}


